package com.blazingredfireball.rouleurlog.ria.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.blazingredfireball.rouleurlog.ria.business.RouteDelegate;
	import com.blazingredfireball.rouleurlog.ria.model.RouleurlogModelLocator;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;

	public class GetRoutesCommand extends BaseCommand implements ICommand, IResponder
	{
		private var model : RouleurlogModelLocator = RouleurlogModelLocator.getInstance();
		
		public function execute( event : CairngormEvent ) : void
		{
			if( RouleurlogModelLocator.getInstance().routes == null )
			{
			    var delegate : RouteDelegate = new RouteDelegate( this );    
			    delegate.getRoutes( model.user.id );
			}
			else
			{
				Alert.show( "Routes already retrieved!" );
			}
		}
		
		public function result( data : Object ) : void
		{
			model.routes = new ArrayCollection( data.result );
		}
		
		public function fault( info : Object ) : void
		{
			var faultEvent : FaultEvent = FaultEvent( info );
			
			if (!checkForAuthenticationFault(faultEvent))
		    {
				Alert.show("Routes could not be retrieved!");
			}
		}
	}
}